package com.wyhello.tube4.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.wyhello.tube4.dao.UserOperateDAO;
import com.wyhello.tube4.pojo.User;

public class UserOperateDAOImpl implements UserOperateDAO {
	private SessionFactory sessionFactory;
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	/**
	 * 用户信息的录入
	 * @param user 要录入的信息
	 * @return 返回是否录入成功
	 */
	@Override
	public boolean saveUser(User user) {
		Session session = sessionFactory.getCurrentSession();
		int i = 1;
		try{
			session.save(user);
		}catch(Exception e){
			i=0;
		}
		if(i==1){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 对用户信息进行批量删除
	 * @param ids 要删除的id
	 * @return 返回删除的条数
	 */
	@Override
	public int deleteUser(String ids) {
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "delete from User where id in "+ids;
		int i = session.createSQLQuery(sql).executeUpdate();
		return i;
	}
	/**
	 * 查询所有的用户信息
	 * @return 返回所有用户信息
	 */
	@Override
	public List<User> queryUser() {
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "from User";
		List<User> list = session.createQuery(sql).list();
		return list;
	}
	/**
	 * 根据id查询用户信息
	 * @param id 要查询的id
	 * @return 为Id的用户信息
	 */
	@Override
	public User getUserById(int id) {
		Session session  =  sessionFactory.getCurrentSession();
		User user = (User) session.get(User.class, Integer.valueOf(id));
		return user;
	}
	/**
	 * 对用户信息进行修改
	 * @param tube 修改后的信息
	 * @return 修改是否成功
	 */
	@Override
	public boolean updateUser(User user) {
		Session session  =  sessionFactory.getCurrentSession();
		try{
			session.update(user);
		}catch(Exception e){
			return false;
		}
		return true;
	}
	@Override
	public void deleteUS(User user) {
		Session session  =  sessionFactory.getCurrentSession();
		session.delete(user);
		
	}

}
